<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>reactive-基础用法</title>
</head>

<body>


  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <div id="Application">
    <div class="demo">
      <div>姓名：{{state.name}}</div>
      <div v-if="state.age>0">年龄：{{state.age}}</div>
      <div>
        爱好：
        <span v-for="items in state.hobby">{{items}}，</span>
      </div>
      <div>
        地址：{{state.address.provoince}} - {{state.address.city}} -
        {{state.address.street}}
      </div>
    </div>

    <div class="demo">
      <div>学校：{{state.school||'自学成才'}}</div>
    </div>
  </div>
  <script>
    const App = Vue.createApp({
      setup() {
        // 响应式对象
        const state = Vue.reactive({
          name: "太凉",
          age: 18,
          hobby: ["游泳", "爬山"],
          address: {
            provoince: "北京",
            city: "北京直辖市",
            street: "东城区长安街",
          },
        });

        // 过3秒后改变
        setTimeout(() => {
          // update1:改变name属性
          state.name = "冰箱太凉";

          // update2:深度改变 address属性
          state.address.provoince = "山东省";
          state.address.city = "临沂市";

          // update3：新增 school属性
          state.school = "清华北大";

          // update4：删除 年龄属性
          delete state.age;

          // update5：数组 添加一项
          state.hobby.push("打豆豆");
        }, 3000);

        return {
          //注意这里不能通过 ...state 方式结构，这样会丢失响应式
          state,
        };
      },
    });
    App.mount("#Application");
  </script>
</body>

</html>